﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Pibby.Core;
using Pibby.Core.IO;
using Pibby.Core.PortableMedia;
using Pibby.Core.Views;
using Pibby.Core.ViewModels;
using invrt;


namespace Pibby.UI
{
	public partial class PortableDeviceView : UserControl 
	{
		#region IPortableMediaView

		public void Start()
		{
			this.transferButton.Enabled = false;
			this.ejectButton.Enabled = false;

			this.NameLabel.Text = "Name";
			this.CapacityLabel.Text = "Capacity";
			this.FileCountLabel.Text = "Files";
			this.FolderCountLabel.Text = "Folders";

			this.NameValue.Text = "";
			this.CapacityValue.Text = "";
			this.FileCountValue.Text = "";
			this.FolderCountValue.Text = "";

			this.UsedSpaceLabel.Text = "Used";
			this.AvailableSpaceLabel.Text = "Available";

		}

		public void Stop()
		{
		}

		public void ShowDeviceStatus(PortableMediaViewModel vm)
		{
			this.transferButton.Enabled = vm.Connected;
			this.ejectButton.Enabled = vm.Connected;
			this.playerCapacityView1.Visible = vm.Connected;

			if (vm.Connected)
			{
				this.RefreshDiskInfo(vm);

				this.TransferToDevice();
			}
		}

		public void RefreshDeviceStatus(PortableMediaViewModel vm)
		{
			this.transferButton.Enabled = vm.Connected;
			this.ejectButton.Enabled = vm.Connected;
			this.playerCapacityView1.Visible = vm.Connected;

			if (vm.Connected)
				RefreshDiskInfo(vm);
		}

		#endregion IPortableMediaView

		#region Constructors

		public PortableDeviceView()
		{
			InitializeComponent();
		}

		#endregion Constructors

		public void CheckConnection(MediaPlayerSettings settings)
		{
		}

		public void CheckArrived(RemovableDriveEventArgs e)
		{
		}

		public void CheckRemoved(RemovableDriveEventArgs e)
		{
		}

		public void Transfer()
		{
			this.TransferToDevice();
		}

		private void TransferToDevice()
		{
		}

		private void EjectConnectedDevice()
		{
		}

		public void ReportError(string message)
		{
			MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		public void TransferStarting(int itemsToTransfer)
		{
		}

		public void TransferProgress(int item)
		{
		}

		public void TransferFinished()
		{
		}

		#region Event Handlers

		private void PortableDeviceView_Load(object sender, EventArgs e)
		{

		}

		private void transferButton_Click(object sender, EventArgs e)
		{
			this.TransferToDevice();
		}

		private void ejectButton_Click(object sender, EventArgs e)
		{
			this.EjectConnectedDevice();
		}

		#endregion Event Handlers


		private void RefreshDiskInfo(PortableMediaViewModel vm)
		{
			//this.label1.Visible = vm.Connected;
			//this.label2.Visible = vm.Connected;
			
			//this.label1.Text = vm.VolumeLabel;
			//this.label2.Text = string.Format ("{0} free space {1} pending items", 
			//                                    FileSizeInterpreter.InterpretSize(vm.AvailableSpace),
			//                                    vm.PendingItems);

			this.NameLabel.Text = "<em>Name</em>";
			this.CapacityLabel.Text = "<em>Capacity</em>";
			this.FileCountLabel.Text = "<em>Files</em>";
			this.FolderCountLabel.Text = "<em>Folders</em>";

			this.NameValue.Text = vm.VolumeLabel;
			this.CapacityValue.Text = FileSizeInterpreter.InterpretSize(vm.TotalSize);

			var driveFactory = InversionOfController.Factory.CreateTypeOf<IDriveFactory>();
			var drive = driveFactory.GetDriveByLabel(vm.VolumeLabel);
			var folderFactory = InversionOfController.Factory.CreateTypeOf<IFolderFactory>();
			var rootFolder = folderFactory.GetFolder(drive.Name);

			var stats = new FolderContentStatistician();

			stats.ListContentsOf(rootFolder, System.IO.SearchOption.AllDirectories);

			this.FileCountValue.Text = string.Format("{0} files", stats.FileCount);
			this.FolderCountValue.Text = string.Format("{0} folders", stats.FolderCount);

			this.UsedSpaceLabel.Text = string.Format("<em>Used</em> {0}", FileSizeInterpreter.InterpretSize(vm.TotalSize - vm.AvailableSpace));
			this.AvailableSpaceLabel.Text = string.Format("<em>Available</em> {0}", FileSizeInterpreter.InterpretSize(vm.AvailableSpace));

			this.playerCapacityView1.SetDrive(vm.AvailableSpace, vm.TotalSize);
		}


	}
}
